package com.kx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kx.bean.Car;
import com.kx.bean.ParkingSpace;
import com.kx.bean.User;
import com.kx.dao.CarMapper;
import com.kx.dao.ParkingSpaceMapper;
import com.kx.dao.UserMapper;
import com.kx.service.ParkingSpaceService;
import com.kx.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParkingSpaceServiceImpl implements ParkingSpaceService {

    @Autowired
    private ParkingSpaceMapper parkingSpaceMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CarMapper carMapper;

    @Override
    public DataVO<ParkingSpace> findData(Integer page, Integer limit) {
        DataVO<ParkingSpace> dataVO = new DataVO<>();
        //1、设置code
        dataVO.setCode(0);
        //2、设置msg
        dataVO.setMsg("");
        //3、设置count(如果使用了mybatis-plus分页，则不需要调用其中selectCount方法单独调用)
//        dataVO.setCount(UserMapper.selectCount(null));
//        List<User> UserList= UserMapper.selectList(null);
        //取消上方全查询，分页设置List<User>通过getRecords()获取
        IPage<ParkingSpace> parkingSpaceIPage = new Page<>(page, limit);
        QueryWrapper<ParkingSpace> wrapper = new QueryWrapper<>();
        wrapper.eq("parking_status", 0);
        IPage<ParkingSpace> result = parkingSpaceMapper.selectPage(parkingSpaceIPage, wrapper);
        dataVO.setCount(result.getTotal());
        List<ParkingSpace> parkingSpaceList = result.getRecords();
        //4、设置data
        dataVO.setData(parkingSpaceList);
        return dataVO;
    }

    @Override
    public Integer findParkingSpaceCount() {
        return parkingSpaceMapper.selectCount(null);
    }

    @Override
    public Integer findReleaseSpace() {
        QueryWrapper<ParkingSpace> wrapper = new QueryWrapper<>();
        wrapper.eq("parking_status", 0);
        Integer integer = parkingSpaceMapper.selectCount(wrapper);
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("remark", "").ne("remark", null);
        Integer integer1 = carMapper.selectCount(queryWrapper);
        return integer - integer1;
    }

    @Override
    public ParkingSpace findById(Integer pid) {
        return parkingSpaceMapper.selectByPrimaryKey(pid);
    }

    @Override
    public int updateSpaceById(ParkingSpace ps) {
        ps.setParkingStatus("1");
        return parkingSpaceMapper.updateByPrimaryKeySelective(ps);
    }

    @Override
    public int updateSpace(ParkingSpace ps) {
        UpdateWrapper<ParkingSpace> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("pid", ps.getPid());
        return parkingSpaceMapper.update(ps, updateWrapper);
    }

    @Override
    public int deleteSpace(Integer pid) {
        QueryWrapper<ParkingSpace> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        return parkingSpaceMapper.delete(queryWrapper);
    }

    @Override
    public int insertSpace(ParkingSpace space) {
        return parkingSpaceMapper.insert(space);
    }
}
